package com.yz.app.gsmdata.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yz.app.gsmdata.entity.Gsmdata;
import com.yz.app.gsmdata.service.IGsmdataService;
import com.yz.app.gsmdata.service.impl.GsmdataIdJobServiceImpl;
import com.yz.app.gsmdata.service.impl.GsmdataServiceImpl;
import com.yz.app.gsmdata.util.BaseTools;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yuanzheng
 * @version 1.0
 * @since 2020-12-24
 */
@Component
public class HistoryComponent {

    @Resource
    private GsmdataServiceImpl gsmdataService;

    @Resource
    private DataSourceTransactionManager dataSourceTransactionManager;

    public void putData() {
        //手机号数量
        int mobileCount = 10000;
        //设备数量
        int ipCount = 10000;
        //生成数据的数量
        int dataCount = 10000;
        //获取手机号信息
        List<Map> listMobile = getMobile(mobileCount);
        //获取设备信息
        List<Map> listIp = getip(ipCount);
        //保存数据
        saveData("2021-04-14", listMobile, listIp, dataCount);
    }

    //保存数据
    public void saveData(String day, List<Map> listMobile, List<Map> listIp, int count) {
        List<Gsmdata> gsmdataList = new ArrayList<>();
        for (int j = 0; j < count; j++) {
//            Map mobielMap = listMobile.get((int) (Math.random() * listMobile.size()));
//            Map ipMap = listIp.get((int) (Math.random() * listIp.size()));
            Map mobielMap = listMobile.get(j);
            Map ipMap = listIp.get(j);
            Gsmdata gsmd = new Gsmdata();
            gsmd.setId(String.valueOf(j + 1));
            gsmd.setIp(ipMap.get("ip").toString());
            gsmd.setLocation(ipMap.get("location").toString());
            gsmd.setImei(mobielMap.get("imei").toString());
            gsmd.setImsi(mobielMap.get("imei").toString());
            gsmd.setPhonenum(mobielMap.get("mobile").toString());
            gsmd.setAttribution(mobielMap.get("attribution").toString());
            gsmd.setTme(new Date(BaseTools.randomDateTime(day + " 00:00:00", day + " 23:59:59")));
            gsmdataList.add(gsmd);
        }
        gsmdataService.saveBatch(gsmdataList);
    }

    //获取设备数据
    public List<Map> getip(int count) {
        List<Map> list = new ArrayList<Map>();
        for (int i = 0; i < count; i++) {
            list.add(ArrayUtils.toMap(new String[][]{
                    {"ip", String.valueOf(i)},
                    {"location", "河南省郑州市中原区金属西路第" + i + "号"},
            }));
        }
        return list;
    }

    //获取count个手机信息
    public List<Map> getMobile(int count) {
        List<Map> list = new ArrayList<Map>();
        long mobile = 13000000000L;
        long imsi = 460000000000000L;
        long imei = 860000000000000L;
        for (int i = 0; i < count; i++) {
            list.add(ArrayUtils.toMap(new String[][]{
                    {"imsi", String.valueOf(imsi + i)},
                    {"imei", String.valueOf(imei + i)},
                    {"mobile", String.valueOf(mobile + i)},
                    {"attribution", String.valueOf((int) (Math.random() * 1000))}
            }));
        }
        return list;
    }


}



